package aod.android.com.bernay.message.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseFragment;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.message.adapter.PagerAdapter;
import aod.android.com.bernay.util.MessageEvent;
import aod.android.com.bernay.util.Utils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.NoScrollViewPager;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/3/31 15:26
 * @Email jamesKing0812@gmail.com
 */
public class MessageFragment extends BernayBaseFragment implements View.OnClickListener {

    @HiView(R.id.tb_layout)
    TabLayout mTabLayout;

    @HiView(R.id.ViewPager)
    NoScrollViewPager mViewPager;

    @HiView(R.id.iv_delete)
    ImageView mDelete;

    private PagerAdapter mPagerAdapter;
    private String[] titles = {"系统消息","设备消息"};
    private List<Fragment> fragments;

    private SystemMessageFragment systemMessageFragment;

    private DeviceMessageFragment deviceMessageFragment;
    @Override
    protected int initLayout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initData() {
        initViewPagerFragment();
    }

    private void initViewPagerFragment() {
        systemMessageFragment = new SystemMessageFragment();
        deviceMessageFragment = new DeviceMessageFragment();
        mPagerAdapter = new PagerAdapter(getChildFragmentManager());
        fragments = new ArrayList<>();
        fragments.add(systemMessageFragment);
        fragments.add(deviceMessageFragment);
        mPagerAdapter.setTitles(titles);
        mPagerAdapter.setFragments(fragments);
        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
        mDelete.setOnClickListener(this);

        if(mTabLayout != null) {
            TabLayout.Tab tab1 = mTabLayout.getTabAt(0);
            if (tab1 != null) {
                tab1.setCustomView(Utils.getTabView0(getActivity(),titles[0]));
            }
            TabLayout.Tab tab2 = mTabLayout.getTabAt(1);
            if (tab2 != null) {
                tab2.setCustomView(Utils.getTabView1(getActivity(),titles[1]));
            }
        }

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("设备消息")){
                    deviceMessageFragment.deviceMsgList();
                    mDelete.setVisibility(View.GONE);
                }else{
                    systemMessageFragment.getUserMsg();
                    mDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_delete:
                Toast.makeText(getActivity(),"清空消息",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
